package org.takeback.chat.utils;

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.net.URL;

import javax.imageio.ImageIO;

import org.apache.commons.lang3.StringUtils;

public class ExclusivePostersUtil {
    private final static int imageWidth = 750;  //图片的宽度
    private final static int imageHeight = 1280; //图片的高度
//    
//    public static void main(String[] args) throws IOException {
//    	byte[] datas = getShareImage("http://image.aixingongce.com/icon/pddhaibao04.png", 
//    			"用BML轻松购", 124, 80, 70, 255, 255, 255, 
//    			"http://image.aixingongce.com/temp/201810180048b45312-189b-4a14-888f-4a49a0811d91.jpg", 
//    			240, 940, 226);
//		FileUpload.saveFile(datas, "D://", "jpg");
//	}
//    
    public static byte[] getShareImage(String backgroud,  
    		String qrcodeBG, int ewmX, int ewmY, int ewmWidth) {
    	BufferedImage image = new BufferedImage(imageWidth, imageHeight, BufferedImage.TYPE_INT_RGB);
        Graphics2D main = image.createGraphics();
        main.setColor(Color.white);
        main.fillRect(0, 0, imageWidth, imageHeight);
        
        //***********************插入中间广告图
        Graphics mainPic = image.getGraphics();
        mainPic.setColor(Color.white);
        mainPic.fillRect(0, 0, imageWidth, imageHeight);
        BufferedImage temp = null;
        try {
        	URL url = new URL(backgroud);
        	temp = ImageIO.read(url); 
            mainPic.drawImage(temp, 0, 0, imageWidth, imageHeight, null);
        } catch (Exception e) {
        	e.printStackTrace();
        }
        try {
        	int space = (266 - ewmWidth)/2;
        	mainPic.fillRoundRect(ewmX, ewmY, 266, 266, 10, 10);
        	URL url = new URL(qrcodeBG);  
        	temp = ImageIO.read(url); 
            mainPic.drawImage(temp, ewmX+space, ewmY+space, ewmWidth, ewmWidth, null);
        } catch (Exception e) {
        }
        
        mainPic.dispose();
        
        ByteArrayOutputStream baoStr = new ByteArrayOutputStream();
        try {
			ImageIO.write(image, "jpg", baoStr);
		} catch (IOException e) {
			e.printStackTrace();
		}
        return baoStr.toByteArray();
    }
    
    private static void drawFont(Graphics g, int mode, int fontSize, Color color, String text, int x, int y) {
    	Font font = new Font("宋体", mode, fontSize);
        g.setFont(font);
        g.setColor(color);
        g.drawString(text, x, y);
    }
    
}